package com.example.lenovo0509;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lenovo0509.bean.NewsInfoBean;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class NewsListAdapter extends BaseAdapter {
    private static final String TAG = "NewsListAdapter";
    Context context;
    List<NewsInfoBean.ResultBean.DataBean> data;
    List<String>deleteNews;//存储删除的新闻信息

    public NewsListAdapter(Context context, List<NewsInfoBean.ResultBean.DataBean> data, List<String> deleteNews) {
        this.context = context;
        this.data = data;
        this.deleteNews = deleteNews;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.item_news_list,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        NewsInfoBean.ResultBean.DataBean dataBean = data.get(position);
        String authorName = dataBean.getAuthor_name();
        String date = dataBean.getDate();
        String title = dataBean.getTitle();
        holder.authorTv.setText(authorName);
        holder.titleTv.setText(title);
        holder.timeTv.setText(date);
        String pic1 = dataBean.getThumbnail_pic_s();
        String pic2 = dataBean.getThumbnail_pic_s02();
        String pic3 = dataBean.getThumbnail_pic_s03();
        if (TextUtils.isEmpty(pic1)) {
           holder.iv1.setVisibility(View.GONE);
        }else {
            holder.iv1.setVisibility(View.VISIBLE);
            Picasso.with(context).load(pic1).into(holder.iv1);
        }
        //第二章图片
        if (TextUtils.isEmpty(pic1)) {
            holder.iv2.setVisibility(View.GONE);
        }else {
            holder.iv2.setVisibility(View.VISIBLE);
            Picasso.with(context).load(pic2).into(holder.iv2);
        }
        //第三张图片
        if (TextUtils.isEmpty(pic1)) {
            holder.iv3.setVisibility(View.GONE);
        }else {
            holder.iv3.setVisibility(View.VISIBLE);
            Picasso.with(context).load(pic3).into(holder.iv3);
        }
        holder.deleteIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 data.remove(position);
                 notifyDataSetChanged();
            }
        });

        return convertView;
    }
    class ViewHolder{
        TextView titleTv,timeTv,authorTv;
        ImageView iv1,iv2,iv3,deleteIv;
        public ViewHolder(View view){
            titleTv=view.findViewById(R.id.item_news_title);
            timeTv=view.findViewById(R.id.item_news_time);
            authorTv=view.findViewById(R.id.item_news_author);
            iv1=view.findViewById(R.id.item_news_iv1);
            iv2=view.findViewById(R.id.item_news_iv2);
            iv3=view.findViewById(R.id.item_news_iv3);
            deleteIv=view.findViewById(R.id.item_news_delete);

        }
    }
}
